#include <myheader.h>

/*Usage:  */
int main(int argc, char *argv[])
{
    char *ip = "192.168.112.129";
    char *port = "8080";

    //make a socket obj
    int socket_fd = socket(AF_INET,SOCK_STREAM,IPPROTO_TCP);
    ERROR_CHECK(socket_fd, -1, "make socket obj");

    //make a sockaddr struction
    struct sockaddr_in sockaddr;
    sockaddr.sin_family = AF_INET;
    sockaddr.sin_port = htons(atoi(port));
    sockaddr.sin_addr.s_addr = inet_addr(ip);
    
    int ret = bind(socket_fd,(struct sockaddr*)&sockaddr,sizeof(sockaddr));
    ERROR_CHECK(ret,-1,"bind");


    //listen
    ret = listen(socket_fd,10);
    ERROR_CHECK(ret,-1,"listen");

    //accept
    struct sockaddr socket_info;
    socklen_t addrlen = sizeof(socket_info);
    int net_fd = accept(socket_fd,&socket_info,&addrlen);

    //printf the client info
    printf("client ip = %s\n",socket_info.sa_data);
    if(socket_info.sa_family == AF_INET){
        printf("ipv4\n");
    }
    ERROR_CHECK(ret,-1,"accept");


    //recvive
    char buf[60] = {0};
    ssize_t recv_cnt = recv(net_fd,buf,sizeof(buf),0);
    ERROR_CHECK(recv_cnt,-1,"recv");

    //printf
    printf("%s\n",buf);


    //close
    close(net_fd);
    close(socket_fd);

    return 0;
}

